package com.sos.core.game
{
	import com.sos.core.DataObject;
	import com.sos.logging.Logger;

	public class GamePlayerProc extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// Property names
		protected static const		JSON_PROPERTY_PLAYERID:String			= "playerId";
		protected static const		JSON_PROPERTY_ABILITIES:String			= "abilities";
		
		// Default values
		protected static const      DEFAULT_PLAYERID:uint					= DataObject.INVALID_ID;
		
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var	_playerId:uint;
		private var _abilities:Vector.<GameProc>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GamePlayerProc(data:Object = null)
		{
			super();
			// Init objects
			_abilities = new Vector.<GameProc>();
			// clears data
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get playerId():uint 
		{
			return _playerId;
		}
		
		public function set playerId(value:uint):void 
		{
			_playerId = value;
		}
		
		
		public function get procs():Vector.<GameProc> 
		{
			return _abilities;
		}
		
		public function set procs(value:Vector.<GameProc>):void 
		{
			_abilities = value;
		}
		
		
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		override protected function validate(data:Object):void
		{
			// Base properties
			if (!data.hasOwnProperty(JSON_PROPERTY_PLAYERID)) 			throwLoadMissingPropError(JSON_PROPERTY_PLAYERID);
			
		}
		
		override protected function fill(data:Object):void
		{			
			
			_playerId = int(data[JSON_PROPERTY_PLAYERID]);
			
			// Temporary effects list
			var abilities:Vector.<GameProc> = new Vector.<GameProc>;
			// Check if JSON_PROPERTY_EFFECTS exists 
			if (data.hasOwnProperty(JSON_PROPERTY_ABILITIES))
			{
				
				// Loop over raw effects
				for each (var effect:Object in data[JSON_PROPERTY_ABILITIES])
				{
					abilities.push(new GameProc(effect));
				}
			}
			// Clear and popolate effects list
			_abilities.splice(0, _abilities.length);
			for (var i:int = 0; i < abilities.length; i++)
			{
				_abilities.push(abilities[i]);
			}
			
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			// Clear data
			_playerId	= DEFAULT_PLAYERID;
			
			// Clear objects
			_abilities.splice(0, _abilities.length);
		}


	}
}